﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Xml;
using System.IO;
using System.IO.IsolatedStorage;


namespace TP2
{
    public class ClassConnection
    {
        
        string user_name;
        string password;
        string data_source;
        string conString;
        string flag;
        public ClassConnection()
        {
            IsolatedStorageFile isoStorage;
            isoStorage = IsolatedStorageFile.GetUserStoreForDomain();
            String[] StoreFileNames;
            String StoreFile;
            StoreFileNames = isoStorage.GetFileNames("UserSettingsXML.xml");
            for (int i = 0; i < StoreFileNames.Length; i++)
                if (StoreFileNames[i].Equals("UserSettingsXML.xml"))
                {
                    StreamReader stmReader = new StreamReader(new IsolatedStorageFileStream("UserSettingsXML.xml", FileMode.Open, isoStorage));
                    XmlTextReader xmlReader = new XmlTextReader(stmReader);
                    while (xmlReader.Read())
                    {
                        switch (xmlReader.Name)
                        {
                            case "user_name":
                                this.user_name = xmlReader.ReadString();
                                break;
                            case "password":
                                this.password = xmlReader.ReadString();
                                break;
                            case "data_source":
                                this.data_source = xmlReader.ReadString();
                                break;
                            case "save_flag":
                                flag = xmlReader.ReadString();
                                break;
                        }
                    }
                    xmlReader.Close();
                    stmReader.Close();
                }
            this.conString="User Id="+user_name+";"+"Password="+password+";"+"Data Source="+data_source;
        }

        public void WriteSettings(string name, string pass, string data_source, bool flag)
        {
            IsolatedStorageFile isoStorage;
            isoStorage = IsolatedStorageFile.GetUserStoreForDomain();
            IsolatedStorageFileStream stmWriter = new IsolatedStorageFileStream("UserSettingsXML.xml", FileMode.Create, isoStorage);
            XmlTextWriter writer = new XmlTextWriter(stmWriter, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("UserSettings");
                writer.WriteStartElement("user_name");
                writer.WriteString(name);
                writer.WriteEndElement();
                writer.WriteStartElement("password");
                writer.WriteString(pass);
                writer.WriteEndElement();
                writer.WriteStartElement("data_source");
                writer.WriteString(data_source);
                writer.WriteEndElement();
                writer.WriteStartElement("save_flag");
                if (flag==true)
                {
                    string str = "true";
                    writer.WriteString(str);
                }
                else
                {
                    string str = "false";
                    writer.WriteString(str);
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
                stmWriter.Close();
                isoStorage.Close();
            }
        }

        public string InitConString()
        {
            return this.conString;
        }

        public OracleConnection GetConnection()
        {
            OracleConnection connection = new OracleConnection(this.conString);
            connection.Open();
            return connection;
        }

        public void CloseConnection(OracleConnection con)
        {
            OracleConnection connection = con;
            connection.Close();
        }

        public string[] GetSettings()
        {
            string[] setting_mas = new string[4];
            setting_mas[0] = user_name;
            setting_mas[1] = password;
            setting_mas[2] = data_source;
            setting_mas[3] = flag;
            return setting_mas;
        }
     

    }
}
